#include "pch.h"
#include "AirplanLoading.h"


namespace LoadingWK
{
    AirplanLoading::AirplanLoading()
    {
        _pIntelligentLoad = new gaeaway_load::IntelligentLoad();
    }

    AirplanLoading::~AirplanLoading()
    {
        if (nullptr != _pIntelligentLoad)
        {
            delete _pIntelligentLoad;
            _pIntelligentLoad = nullptr;
        }
    }

    bool AirplanLoading::importInformationOfEquipments(Generic::List<LoadingWK::Equipment^>^ pEquipmentList)
    {
        if (pEquipmentList == nullptr){   return false;}
        
        //Generic::List<LoadingWK::Equipment^> equipmentList = pEquipmentList->GetHashCode();
        gaeaway_load::EquipmentsOfInput equipments(static_cast<size_t>(pEquipmentList->Count));
        for (int i =0 ; i < pEquipmentList->Count ; i++ )
        {
            LoadingWK::Equipment^ pEquipment = pEquipmentList[i];
            gaeaway_load::EquipmentOfInput geaEquip;
            geaEquip.id = pEquipment->_id;
            geaEquip.length = pEquipment->_length;
            geaEquip.width = pEquipment->_width;
            geaEquip.height = pEquipment->_height;

            geaEquip.gravity.y = pEquipment->_gravityY;
            geaEquip.weight = pEquipment->_weight;

            Generic::List<LoadingWK::AxleLoad^>^ axleLoadList = pEquipment->_axleLoadList;
            if (axleLoadList == nullptr) { continue; }

            for (int j = 0 ; j < axleLoadList->Count ; j++)
            {
                LoadingWK::AxleLoad^ pAxleLoad = axleLoadList[j];
                if (pAxleLoad==nullptr){   continue;}

                gaeaway_load::AxleLoad axleLoad;
                axleLoad.offsetenth = pAxleLoad->_offsetenth;
                axleLoad.weight = pAxleLoad->_weight;
                geaEquip.axleLoadList.emplace_back(axleLoad);
            }

            equipments.emplace_back(geaEquip);
        }
        return _pIntelligentLoad->importEquipments(equipments);
    }

    bool AirplanLoading::importInformationOfAirplane(LoadingWK::Airplane^ airplane, Double minSpacing)
    {
        gaeaway_load::Airplane airplaneT;
        airplaneT.length = airplane->_length;
        airplaneT.width = airplane->_width;
        airplaneT.height = airplane->_height;

        airplaneT.gravity.y = airplane->_gravityY;
        airplaneT.minY = airplane->_minY;
        airplaneT.maxY = airplane->_maxY;
        airplaneT.weight = airplane->_weight;
        airplaneT.maxLoadWeight = airplane->_maxLoadWeight;
        airplaneT.locationTypeOfStartLoading = airplane->_locationTypeOfStartLoading;
        _pIntelligentLoad->importAirplane(airplaneT, minSpacing);

        gaeaway_load::WeightLimitOfAreas weightLimitOfAreas;
        Generic::List<LoadingWK::WeightLimit^>^ pWeightLimitList =  airplane->_weightLimitOfAreaList;
        for (int i = 0; i < pWeightLimitList->Count; i++)
        {
            LoadingWK::WeightLimit^ pWeight = pWeightLimitList[i];

            gaeaway_load::WeightLimitOfArea wla;
            wla.yRange.min = pWeight->_leftY;
            wla.yRange.max = pWeight->_rightY;
            wla.maxLoadingWeight = pWeight->_maxLoadingWeight;
            weightLimitOfAreas.emplace_back(wla);
        }

        return true;
    }

    LoadingWK::LoadedEquipmentList^ AirplanLoading::startLoading(int maxCountOfAirplane)
    {
        LoadingWK::LoadedEquipmentList^ pEquipmentList = gcnew LoadingWK::LoadedEquipmentList();
        
        auto result = _pIntelligentLoad->startLoading(maxCountOfAirplane);
        pEquipmentList->_countOfLoadedAirplane = _pIntelligentLoad->getCountOfLoadedAirplanes();
        for (auto ver : result)
        {
            LoadingWK::LoadedEquipment^ equipment =
                gcnew LoadingWK::LoadedEquipment(ver.airplanIndex, ver.equipmentID, ver.leftY);
            
            pEquipmentList->addEquipment(equipment);
        }
        return pEquipmentList;
    }
}